import re
import xlwt
from selenium import webdriver
import time
import requests

class QCWY:

    def __init__(self,keyword,city,maxpagenum):
        self.keyword = keyword
        self.city = city
        self.maxpagenum = maxpagenum

    def run(self):

        # 放到excl表格中
        # 创建一个workbook 设置编码
        workbook = xlwt.Workbook(encoding='utf-8')
        # 创建一个worksheet
        worksheet = workbook.add_sheet('QCWY', cell_overwrite_ok=True)
        # 写入excel
        # 参数对应 行, 列, 值
        worksheet.write(0, 0, label='职位')
        worksheet.write(0, 1, label='发布时间')
        worksheet.write(0, 2, label='薪水待遇')
        worksheet.write(0, 3, label='公司名称')

        url = f'https://search.51job.com/list/0{QCWY.city_num()}00,000000,0000,00,9,99,{self.keyword},2,1.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare='

        driver = requests.get(url)
        driver_text = driver.content.decode('gbk')

        zw =  re.findall(r"<dt>Player count</dt><dd>(.+?)</dd>", driver_text)


        i = 1
        for pageNo in range(1,self.maxpagenum+1):
            driver.find_element_by_id('jump_page').click()
            driver.find_element_by_id('jump_page').clear()
            driver.find_element_by_id('jump_page').send_keys(pageNo)
            driver.find_element_by_class_name('og_but').click()
            time.sleep(1)
            #对每一个页面进行操作
            jobs = driver.find_elements_by_css_selector('.j_joblist > .e')
            for job in jobs:
                # 这是职位的
                position = job.find_element_by_css_selector('.j_joblist > .e >a >.t > span:nth-child(1)')
                # 这是发布时间
                release_time = job.find_element_by_css_selector('.j_joblist > .e >a >.t > span:nth-child(2)')
                # 这是薪水
                salary = job.find_element_by_css_selector('.j_joblist > .e > a > .info > .sal')
                # 公司名称
                corporate_name = job.find_element_by_css_selector('.j_joblist >.e > .er > a')

                # 写入excel
                # 参数对应 行, 列, 值
                worksheet.write(i, 0, label=position.text)
                worksheet.write(i, 1, label=release_time.text)
                worksheet.write(i, 2, label=salary.text)
                worksheet.write(i, 3, label=corporate_name.text)
                i = i+1
                # 打印出来
                print(position.text, release_time.text, salary.text, corporate_name.text)
            if pageNo == self.maxpagenum:  #一样的，当爬到的页数是当前的一页时候，结束（不是全部哈，下面那个才是全部）
                print('搜索完毕')
                # 保存
                workbook.save('前程无忧招聘信息.xls')
                break

            #判断是否到了最后一页,搜索最大值re.findall('(\w*[0-9]+)\w*', driver.find_element_by_xpath('/html/body/div[2]/div[3]/div/div[2]/div[4]/div[2]/div/div/div/span[1]').text)
            num = re.findall('(\w*[0-9]+)\w*', driver.find_element_by_xpath(
                '/html/body/div[2]/div[3]/div/div[2]/div[4]/div[2]/div/div/div/span[1]').text)#提取列表中的数字，这里得到的是一个列表并且列表中的数字是字符类型
            num = list(map(int, num))#将列表中的字符串类型变成数字类型
            if pageNo == num[0]:
                print('搜索完毕')
                # 保存
                workbook.save('前程无忧招聘信息.xls')
                break

    def city_num(self):
        res = requests.get('https://js.51jobcdn.com/in/js/2016/layer/area_array_c.js?20200818')
        part1 = res.text.split('area=')[1].split(';')[0]
        part = eval(part1)

        part = {v:k for k,v in part.items()}
        while self.city not in part:
            self.city = input(f'没有{self.city}这个城市请重新输入(输入完毕后加回车)')
        return part[self.city]


    #对一个页面进行输出
    # def onepageshow(self,driver):
    #     # 处理每一页的线性
    #     jobs = driver.find_elements_by_css_selector('.j_joblist > .e')
    #     for job in jobs:
    #         # 这是职位的
    #         position = job.find_element_by_css_selector('.j_joblist > .e >a >.t > span:nth-child(1)')
    #         # 这是发布时间
    #         release_time = job.find_element_by_css_selector('.j_joblist > .e >a >.t > span:nth-child(2)')
    #         # 这是薪水
    #         salary = job.find_element_by_css_selector('.j_joblist > .e > a > .info > .sal')
    #         # 公司名称
    #         corporate_name = job.find_element_by_css_selector('.j_joblist >.e > .er > a')
    #         # 打印出来
    #         print(position.text, release_time.text, salary.text, corporate_name.text)

 # #放到excl表格中
            # # 创建一个workbook 设置编码
            # workbook = xlwt.Workbook(encoding='utf-8')
            # # 创建一个worksheet
            # worksheet = workbook.add_sheet('My Worksheet')

            # 写入excel
            # 参数对应 行, 列, 值
            # worksheet.write(1, 0, label='this is test')
            #
            # # 保存
            # workbook.save('前程无忧招聘信息.xls')

print('这里是前程无忧爬虫系统，通过关键字，城市，搜索界面的最大页数来生成招聘信息excel表，')
msg = input('输入搜索的关键字（比如python输入完按回车）')
city = input('输入城市（比如上海输入完按回车）')
wangtpage = int(input('输入想要爬到的最大页数（不输入值默认全部,你最好给我输入1不然卡得很，输入完按回车）'))
print('....................................')
print('别急一会就生成excel表格，正在抓取信息......')
print('....................................')
print('')
print('....................................')
print('在等一会')
print('....................................')
QCWY(msg,city,wangtpage).run()
